package com.engine.api.dckj.ranking.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.engine.api.dckj.mh.service.UserLocationService;
import com.engine.api.dckj.mh.service.impl.UserLocationServiceImpl;
import com.engine.api.dckj.mh.entity.UserLocationInfo;
import com.engine.api.dckj.ranking.entity.RankingInfo;
import com.engine.api.dckj.ranking.service.RankingService;
import com.engine.api.dckj.ranking.service.impl.RankingServiceImpl;
import com.wbi.util.ParamUtil;
import com.wbi.util.Util;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import java.util.*;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class RankingAction {
    private static Logger logger = Logger.getLogger(RankingAction.class.getName());
    RankingService rankingService = new RankingServiceImpl();
    @POST
    @Path("/getRankingList")
    // 获取部门下所有人员id，name
    public String getRankingList(@Context HttpServletRequest request, @Context HttpServletResponse response){
        JSONObject jsonObject = new JSONObject();
        Map<String,Object> stringObjectMap = ParamUtil.request2Map(request);
        Object deptIdObj = stringObjectMap.get("deptId")==null?"":stringObjectMap.get("deptId");

        logger.info("传入的参数deptId："+ deptIdObj);
        RecordSet recordSet = new RecordSet();
        List<String> childList = new ArrayList<>();
        List<String> ids = new ArrayList<>();
        List<String> idList = new ArrayList<>();
        if(!"".equals(deptIdObj)){// 传部门id
            try {
                childList.add(DepartmentComInfo.getAllChildDepartId(deptIdObj.toString(), deptIdObj.toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            String join = String.join(StrUtil.COMMA, childList);
            idList = getIdList(recordSet, ids, join);
        }else {// 不传部门id
            // 获取登录用户  -- 查询为user为null
            User user = HrmUserVarify.getUser(request, response);
            // 登录用户部门id
            int userDepartment = getUserDepartment(user);
            try {
                childList.add(DepartmentComInfo.getAllChildDepartId(Integer.toString(userDepartment), Integer.toString(userDepartment)));
            } catch (Exception e) {
                e.printStackTrace();
            }
            String join = String.join(StrUtil.COMMA, childList);
            idList = getIdList(recordSet, ids, join);
        }
        String idStr = String.join(StrUtil.COMMA, idList);
        // 月度
        List<RankingInfo> averageWorkingHoursOfMonthList = rankingService.getList(idStr,"averageWorkingHoursOfMonth");
        List<RankingInfo> businessTripDaysOfMonthList = rankingService.getList(idStr,"businessTripDaysOfMonth");
        List<RankingInfo> completeOfMonthList = rankingService.getList(idStr,"completeOfMonth");
        List<RankingInfo> numberOfMonthList = rankingService.getList(idStr,"numberOfMonth");
        List<RankingInfo> incompleteOfMonthList = rankingService.getList(idStr,"incompleteOfMonth");
        jsonObject.put("averageWorkingHoursOfMonthList",averageWorkingHoursOfMonthList);//平均工时
        jsonObject.put("businessTripDaysOfMonthList",businessTripDaysOfMonthList);//出差天数
        jsonObject.put("completeOfMonthList",completeOfMonthList);//工单完成时效
        jsonObject.put("numberOfMonthList",numberOfMonthList);//工单接单数量
        jsonObject.put("incompleteOfMonthList",incompleteOfMonthList);//工单未完成数量
        // 年度
        List<RankingInfo> averageWorkingHoursOfYearList = rankingService.getList(idStr,"averageWorkingHoursOfYear");
        List<RankingInfo> businessTripDaysOfYearList = rankingService.getList(idStr,"businessTripDaysOfYear");
        List<RankingInfo> completeOfYearList = rankingService.getList(idStr,"completeOfYear");
        List<RankingInfo> numberOfYearList = rankingService.getList(idStr,"numberOfYear");
        List<RankingInfo> incompleteOfYearList = rankingService.getList(idStr,"incompleteOfYear");
        jsonObject.put("averageWorkingHoursOfYearList",averageWorkingHoursOfYearList);//平均工时
        jsonObject.put("businessTripDaysOfYearList",businessTripDaysOfYearList);//出差天数
        jsonObject.put("completeOfYearList",completeOfYearList);//工单完成时效
        jsonObject.put("numberOfYearList",numberOfYearList);//工单接单数量
        jsonObject.put("incompleteOfYearList",incompleteOfYearList);//工单未完成数量
        return jsonObject.toString();
    }

    private List<String> getIdList(RecordSet recordSet, List<String> ids, String join) {
        String sql = "select id from hrmresource where departmentid in ("+join+")   and status < 3";
        logger.info("查询部门下所有用户sql："+ sql);
        recordSet.executeQuery(sql);
        while (recordSet.next()) {
            String id = Util.null2String(recordSet.getString("id"));
            ids.add(id);
        }
        return ids;
    }
    private static int getUserDepartment(User user) {
        if(user == null){
            return 0;
        }
        return user.getUserDepartment();
    }
}
